﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SpringStudio.Common;
using SpringStudio.DAL.PaginationHandler;
using Sdepci.InfoCode.BLL.Authority;
using Sdepci.InfoCode.BLL.CodeDesign;

namespace Sdepci.InfoCode.Web.CodeDesign
{
    public partial class TotalInfoList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.ViewState["MyGridTemplate"] = this.MyGridTemplate.InnerHtml;
                this.BindData();
                this.CreateTime1.Attributes.Add("onfocus", "javascript:calendar();");
                this.CreateTime2.Attributes.Add("onfocus", "javascript:calendar();");
            }
        }

        /// <summary>
        /// 绑定数据列表
        /// </summary>
        private void BindData()
        {
            DataSet dsRule = new TypeRuleManager().GetTypeRuleDs();
            this.RuleGuid.DataSource = dsRule.Tables[0];
            this.RuleGuid.DataValueField = "RuleGuid";
            this.RuleGuid.DataTextField = "RuleName";
            this.RuleGuid.DataBind();
            this.RuleGuid.Items.Insert(0, new ListItem("=全部编码类型=", ""));
            this.RuleGuid.Attributes.Add("onchange", "javascript:document.getElementById('btnSearch').click();");

            this.Pagination1.TableName = "InfoCodeInfo";
            this.Pagination1.PrimaryKeys.Add("InfoGuid");
            this.Pagination1.OrderFields.Add(new PaginationOrderField("CreateTime", "Desc"));
            this.Pagination1.SelectCondition = "(1=1)";

            if (this.Request.QueryString["RuleGuid"] != null && this.Request.QueryString["RuleGuid"].Trim().Length > 0)
            {
                this.RuleGuid.SelectedValue = this.Request.QueryString["RuleGuid"].Trim();
                this.Pagination1.SelectCondition += " And RuleGuid=@RuleGuid";
                this.Pagination1.SqlParameters.Add(new PaginationSqlParameter("@RuleGuid", this.Request.QueryString["RuleGuid"].Trim()));
            }

            if (this.Request.QueryString["InfoCode"] != null && this.Request.QueryString["InfoCode"].Trim().Length > 0)
            {
                this.InfoCode.Text = this.Request.QueryString["InfoCode"].Trim();
                this.Pagination1.SelectCondition += " And InfoCode Like @InfoCode";
                this.Pagination1.SqlParameters.Add(new PaginationSqlParameter("@InfoCode", "%" + this.Request.QueryString["InfoCode"].Trim() + "%"));
            }

            if (this.Request.QueryString["CreateTime1"] != null && this.Request.QueryString["CreateTime1"].Trim().Length > 0)
            {
                this.CreateTime1.Text = this.Request.QueryString["CreateTime1"].Trim();
                this.Pagination1.SelectCondition += " And CreateTime >= @CreateTime1";
                this.Pagination1.SqlParameters.Add(new PaginationSqlParameter("@CreateTime1", this.Request.QueryString["CreateTime1"].Trim() + " 00:00:00"));
            }

            if (this.Request.QueryString["CreateTime2"] != null && this.Request.QueryString["CreateTime2"].Trim().Length > 0)
            {
                this.CreateTime2.Text = this.Request.QueryString["CreateTime2"].Trim();
                this.Pagination1.SelectCondition += " And CreateTime <= @CreateTime2";
                this.Pagination1.SqlParameters.Add(new PaginationSqlParameter("@CreateTime2", this.Request.QueryString["CreateTime2"].Trim() + " 23:59:59"));
            }

            if (this.Request.QueryString["CreateUserId"] != null && this.Request.QueryString["CreateUserId"].Trim().Length > 0)
            {
                this.CreateUserId.Text = this.Request.QueryString["CreateUserId"].Trim();
                this.Pagination1.SelectCondition += " And CreateUserId=@CreateUserId";
                this.Pagination1.SqlParameters.Add(new PaginationSqlParameter("@CreateUserId", this.Request.QueryString["CreateUserId"].Trim()));
            }

            this.Pagination1.InitHtml();

            DataSet ds = this.Pagination1.PageDataSet;
            ds.Tables[0].Columns.Add("RuleName");
            Dictionary<string, string> ruleDict = new TypeRuleManager().GetTypeRuleDict();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                if (ruleDict.ContainsKey(row["RuleGuid"].ToString()))
                {
                    row["RuleName"] = ruleDict[row["RuleGuid"].ToString()].ToString();
                }

                row["InfoDesc"] = StringHelper.MakeHtml(row["InfoDesc"].ToString());
            }

            this.MyGridTemplate.InnerHtml = MyGrid.DsToHtml(ds, this.ViewState["MyGridTemplate"].ToString(), this.Pagination1.PageSize, this.Pagination1.PageIndex);

            this.lblTitle.Text += "：" + this.RuleGuid.SelectedItem.Text;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.Request.Form["ItemId"] != null)
            {
                InfoManager infoManager = new InfoManager();
                string[] infoGuids = this.Request.Form["ItemId"].ToString().Split(',');
                bool result = true;
                for (int i = 0; i < infoGuids.Length; i++)
                {
                    if (!infoManager.Delete(infoGuids[i]))
                    {
                        result = false;
                    }
                }
                string msg = result ? "删除完成！" : "操作完成，但未能把所选编码全部删除，请查看错误日志！";
                this.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script language=\"javascript\">alert(\"" + msg + "\");document.location=document.location;</script>");
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string urlParameter = string.Empty;

            if (this.RuleGuid.SelectedValue.Trim().Length > 0)
            {
                urlParameter += "RuleGuid=" + this.RuleGuid.Text.Trim() + "&";
            }

            if (this.InfoCode.Text.Trim().Length > 0)
            {
                urlParameter += "InfoCode=" + this.InfoCode.Text.Trim() + "&";
            }

            if (this.CreateTime1.Text.Trim().Length > 0)
            {
                urlParameter += "CreateTime1=" + this.CreateTime1.Text.Trim() + "&";
            }

            if (this.CreateTime2.Text.Trim().Length > 0)
            {
                urlParameter += "CreateTime2=" + this.CreateTime2.Text.Trim() + "&";
            }

            if (this.CreateUserId.Text.Trim().Length > 0)
            {
                urlParameter += "CreateUserId=" + this.CreateUserId.Text.Trim() + "&";
            }

            if (!"".Equals(urlParameter))
            {
                this.Response.Redirect("TotalInfoList.aspx?" + urlParameter.Substring(0, urlParameter.Length - 1));
            }
            else
            {
                this.Response.Redirect("TotalInfoList.aspx");
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            string[] columns = { "InfoCode", "RuleName", "CreateTime", "CreateUserId" };

            Dictionary<string, string> dictColumns = new Dictionary<string, string>();
            dictColumns.Add("InfoCode", "信息编码");
            dictColumns.Add("RuleName", "编码类型");
            dictColumns.Add("CreateTime", "创建时间");
            dictColumns.Add("CreateUserId", "创建者证档号");

            this.BindData();
            DataSet ds = this.Pagination1.TotalDataSet;

            ds.Tables[0].Columns.Add("RuleName");
            Dictionary<string, string> ruleDict = new TypeRuleManager().GetTypeRuleDict();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                if (ruleDict.ContainsKey(row["RuleGuid"].ToString()))
                {
                    row["RuleName"] = ruleDict[row["RuleGuid"].ToString()].ToString();
                }
            }

            for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
            {
                var column = ds.Tables[0].Columns[i];
                if (dictColumns.ContainsKey(column.ColumnName))
                {
                    column.Caption = dictColumns[column.ColumnName];
                }
                else
                {
                    ds.Tables[0].Columns.Remove(column);
                    i--;
                }
            }

            Session.Add("ExcelDataSet", ds);
            Session.Add("ExcelFileName", this.Page.Title);
            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script language=\"javascript\">window.open('DsToExcel.aspx');document.location=document.location;</script>");
        }
    }
}
